<script setup lang="ts">
import { provide, ref } from 'vue'
import { ModalShowType } from '@/types/common'
import { useData } from './hook'
import { useRenderIcon } from '@/components/ReIcon/src/hooks'

import Search from '@iconify-icons/ep/search'
import Refresh from '@iconify-icons/ep/refresh'
import { useTableHeight } from '@/hooks/useTableHeight'
import NewMcpModal from './new-mcp-modal.vue'

defineOptions({
  name: '/mcpServerMall',
})

const formRef = ref()
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  showSelection,
  selectedRows,
  modalShow,
  mcpId,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
} = useData()
const { tableHeight, tableRef } = useTableHeight()

provide('modalMode', modalShow)
</script>

<template>
  <div class="table-view-layout">
    <el-form ref="formRef" :inline="true" :model="form" class="bg-el-bg w-[99/100] pl-8 pt-4">
      <el-form-item prop="keywords">
        <el-input v-model="form.keywords" placeholder="请输入关键词" clearable class="!w-[200px]" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch()"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)"> 重置</el-button>
      </el-form-item>
    </el-form>

    <PureTableBar @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          @click="
            () => {
              mcpId = null
              modalShow = ModalShowType.New
            }
          "
        >
          新建MCP</el-button
        >
      </template>
      <template #table>
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :max-height="tableHeight"
          :loading="loading"
          :checkList="showSelection ? ['勾选列'] : []"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        />
      </template>
    </PureTableBar>
    <NewMcpModal v-model:modalShow="modalShow" :mcpId="mcpId" :onSearch="onSearch" />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
